<template>
  <Container>
    <template v-slot:table>
      <el-row class="row">
        <el-button type="danger" icon="el-icon-delete" @click="handleClickDel(selectionList)"
          :disabled="selectionList.length == 0" plain>删
          除</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" @click="handleClickAddEdit('add')" plain>添
          加</el-button>
        <el-button type="primary" icon="el-icon-search" @click="queryTableList" plain>查 询</el-button>
      </el-row>
      <el-table :data="tableData" border height="100%"  style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column type="index" width="55" label="序号">
        </el-table-column>
        <el-table-column prop="funcName" label="功能名称">
        </el-table-column>
        <el-table-column prop="deviceTypeName" label="设备类型名称">
        </el-table-column>
        <el-table-column label="查看" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="getFunsFiles(scope.row)">查看文件</el-button>
          </template>
        </el-table-column>
        <el-table-column label="上传文件" width="200">
          <template slot-scope="scope">
            <el-upload class="upload" action="#" :file-list="fileList" :show-file-list="false" :limit="1" :multiple=false
              :auto-upload="false" :on-change="handleChange">
              <el-button type="text" @click="getFunsInfo(scope.row)">上传文件</el-button>
            </el-upload>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" @click="handleClickAddEdit('edit', scope.row)">编辑</el-button>
            <el-button type="text" @click="handleClickDel([scope.row])">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <AddEdit :title="'功能'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisible"
        :data="formData" :deviceTypeList="deviceTypeList">
      </AddEdit>
      <FilesList :title="funcName" @closeDialog="closeDialog" :dialogVisible="dialogVisible_files" :filesList="files"></FilesList>
    </template>
    <template v-slot:pagination>
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="page" :page-sizes="[10, 100, 200, 300, 400, 500, 600]" :page-size="size"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </template>
  </Container>
</template>

<script>
import Container from "@/components/Container"
import AddEdit from './components/addEdit';
import FilesList from './components/filesList';
import deviceTypeFunc from "@/api/deviceTypeFunc";
import deviceType from "@/api/deviceType"
import files from "@/api/files"
export default {
  components: { Container, AddEdit, FilesList },
  data() {
    return {
      deviceTypeFuncId: '',
      fileList: [],
      tableData: [],
      selectionList: [],
      deviceTypeList: [],
      page: 1,
      size: 100,
      total: 10,
      addEditFlag: 'add',
      dialogVisible: false,
      dialogVisible_files: false,
      filesList: [],
      files:[],
      funcName:'',
      formData: {}
    }
  },
  watch: {},
  mounted() {
    this.queryTableList();
    this.queryDeviceTypeList();
  },
  methods: {
    async queryDeviceTypeList() {
      let { data } = await deviceType.queryList();
      if (!data || data.length == 0) {
        // this.$message.warning('请先创建设备类型')
      } else {
        this.deviceTypeList = data || [];
      }
    },
    async getFunsFiles(row) {
      let { data } = await files.queryByFuncId(row.deviceTypeFuncId);
      this.files = data || [];
      if(this.files.length>0){
        this.funcName = row.funcName;
        this.dialogVisible_files = true;
      }else{
        this.$message.warning('该功能未上传文件！')
      }
    },
    getFunsInfo(row) {
      this.deviceTypeFuncId = row.deviceTypeFuncId
    },
    handleChange(file, fileList) {
      console.log(fileList)
      let formData = new FormData();
      formData.append("file", file.raw);
      formData.append('fileName', file.name);
      formData.append('deviceTypeFuncId', this.deviceTypeFuncId);
      files.upload(formData).then(res => {
        if (res.code == 1) {
          this.$message.success('上传成功！')
          this.queryTableList();
        } else {
          this.$message.success(res.msg || '上传失败！')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    queryTableList() {
      let params = {
        page: this.page,
        size: this.size,
        condition: []
      }
      deviceTypeFunc.queryPageList(params).then(res => {
        let { data } = res
        this.total = data.total;
        this.tableData = data.list;
      }).catch(err => {
        console.log(err)
      })
    },
    closeDialog() {
      this.dialogVisible = false;
      this.dialogVisible_files = false;
      this.formData = {};
      this.queryTableList();
    },
    handleClickDel(rowList = []) {
      let list = [];
      rowList.forEach(item => {
        list.push(item.deviceTypeFuncId)
      })
      if (rowList.length == 0) return;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deviceTypeFunc.delList(list).then(res => {
          if (res.code = 1) {
            this.queryTableList();
            this.$message.success('删除成功')
          } else {
            this.$message.warning('删除失败')
          }
        }).catch(err => {
          this.$message.warning('删除失败')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleClickAddEdit(flag = 'add', row = {}) {
      this.formData = JSON.parse(JSON.stringify(row));
      this.addEditFlag = flag
      this.dialogVisible = true;
    },
    handleSelectionChange(list = []) {
      this.selectionList = list
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryTableList();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryTableList();
    },
  }
}
</script>

<style lang="scss" scoped>
.row {
  padding-bottom: 15px;
}
</style>
